﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using ITOMMS.Entity;
using Watson.Core.Tools;
using System.Collections.Generic;
using ITOMMS.Business;

namespace ITOMMS.Web.UserManage
{
    public partial class UserAssetList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.客户.ToString())
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("ID=" + SessionHelper.UserID), ddlCompanyID, "ID", "CompanyName");//绑定公司
                }
                else
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + (int)PubEnums.RoleGroup.客户), ddlCompanyID, "ID", "CompanyName");//绑定公司
                }
                                
                DataShow(GetSearchSql());


                if (SessionHelper.UserRoleGroup != PubEnums.RoleGroup.管理员.ToString())
                {
                    DataControlField colEdit = (DataControlField)this.dgdaoim_sus_plyedr.Columns[10];
                    colEdit.Visible = false;

                    DataControlField colDelete = (DataControlField)this.dgdaoim_sus_plyedr.Columns[11];
                    colDelete.Visible = false;
                }
            }
        }

        private void DataShow(string sql)
        {
            tbCustEmpSoftListEntity entity = new tbCustEmpSoftListEntity();
            AspNetPager1.RecordCount = entity.GetTotalRecords();
            this.dgdaoim_sus_plyedr.DataSource = entity.GetAllByQuery(sql, "order by id desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            dgdaoim_sus_plyedr.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }

        private string GetSearchSql()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
        
            if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.客户.ToString())
            {
                sb.Append(" and 所属公司=" + SessionHelper.UserCompanyID);
            }
            else if (ddlCompanyID.SelectedValue.Trim().Length > 0 && ddlCompanyID.SelectedValue.Trim() != "-1")
            {
                sb.Append(" and  所属公司=").Append(ddlCompanyID.SelectedValue.Trim());
            }

            if (this.ddlEmployee.SelectedValue != "-1" && this.ddlEmployee.SelectedValue!="")
            {
                sb.Append(" and  所属员工=").Append(this.ddlEmployee.SelectedValue.Trim());
            }
            return sb.ToString();
        }

        protected void dgdaoim_sus_plyedr_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //如果是绑定数据行 
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //公司名称
                    int companyid;
                    if (int.TryParse(e.Row.Cells[1].Text, out companyid))
                    {
                        tbUserEntity company = new tbUserEntity();
                        company = company.FindEntity("ID=" + companyid);
                        if (company != null)
                        {
                            e.Row.Cells[1].Text = company.CompanyName;
                        }
                        else
                        {
                            e.Row.Cells[1].Text = "";
                        }
                    }

                    //员工名称
                    int empnameID;
                    if (int.TryParse(e.Row.Cells[2].Text, out empnameID))
                    {
                        tbCustEmployeeInfoEntity emp = new tbCustEmployeeInfoEntity();
                        emp = emp.FindEntity("ID=" + empnameID);
                        if (emp != null)
                        {
                            e.Row.Cells[2].Text = emp.RealName;
                        }
                        else
                        {
                            e.Row.Cells[2].Text = "";
                        }
                    }

                    //主机类型
                    int masterID;
                    if (int.TryParse(e.Row.Cells[3].Text, out masterID))
                    {
                        tb软件清单Entity master = new tb软件清单Entity();
                        master = master.FindEntity("ID=" + empnameID);
                        if (master != null)
                        {
                            e.Row.Cells[3].Text = master.SoftName;
                        }
                        else
                        {
                            e.Row.Cells[3].Text = "";
                        }
                    }





                    int osid;
                    if (int.TryParse(e.Row.Cells[4].Text, out osid))
                    {
                        tb软件清单Entity os = new tb软件清单Entity();
                        os = os.FindEntity("ID=" + osid);
                        if (os != null)
                        {
                            e.Row.Cells[4].Text = os.SoftName;
                        }
                        else
                        {
                            e.Row.Cells[4].Text = "";
                        }
                    }




                    tbCustEmpSoftListEntity empsoftEntity = new tbCustEmpSoftListEntity();
                    empsoftEntity = empsoftEntity.FindEntity("ID=" + dgdaoim_sus_plyedr.DataKeys[0].Value);
                    string QQ = string.Empty;

                    tb软件清单Entity qq1 = new tb软件清单Entity();
                    qq1 = qq1.FindEntity("ID=" + empsoftEntity.聊天软件1.Value);
                    if (qq1 != null)
                    {
                        QQ += qq1.SoftName + ",";
                    }

                    tb软件清单Entity qq2 = new tb软件清单Entity();
                    qq2 = qq2.FindEntity("ID=" + empsoftEntity.聊天软件2.Value);
                    if (qq2 != null)
                    {
                        QQ += qq2.SoftName + ",";

                    }
                    if (QQ.Trim().Length > 0)
                    {
                        QQ = QQ.TrimEnd(',');
                    }
                    e.Row.Cells[5].Text = QQ;

                    int downsoftID;
                    if (int.TryParse(e.Row.Cells[6].Text, out downsoftID))
                    {
                        tb软件清单Entity downsoft = new tb软件清单Entity();
                        downsoft = downsoft.FindEntity("ID=" + downsoftID);
                        if (downsoft != null)
                        {
                            e.Row.Cells[6].Text = downsoft.SoftName;
                        }
                        else
                        {
                            e.Row.Cells[6].Text = "";
                        }
                    }





                    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                    {
                        ((LinkButton)e.Row.Cells[9].FindControl("lbtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                    }
                }
            }
        }

        protected void dgdaoim_sus_plyedr_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.ToString() == "del")
                {
                    if (e.CommandArgument.ToString() != "")
                    {

                        if (new tbCustEmpSoftListEntity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
                            DataShow(GetSearchSql());
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
                        }

                    }
                }
            }
            catch (APPException appex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            }
            catch (Exception ex)
            {
                log4netHelper.error("", ex);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserAssetAdd.aspx", true);
        }

        public string GetName(string param)
        {
            string result = "";
            if (param.Trim() == "-1")
                result = "";
            else
                result = param;
            return result;
        }

        protected void ddlCompanyID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCompanyID.SelectedValue != "-1")
            {
                DropDownListHelper.InitDropList<tbCustEmployeeInfoEntity>(new tbCustEmployeeInfoEntity().GetAllByQuery("CompanyID=" + ddlCompanyID.SelectedValue), ddlEmployee, "ID", "RealName");//绑定客户公司的员工
            }
        }

    }
}
